import sys

from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QMenu, QAction, QDialog, QListWidget, \
    QListWidgetItem
from PyQt5.QtCore import Qt
from PyQt5 import QtCore


class LogList(QListWidget):
    __maxLogLineNum = 1000

    def __init__(self, parent=None):
        super(LogList, self).__init__(parent)
        # 创建右键菜单
        self.popupMenu = QMenu(self)
        self.actionClear = QAction("清除", self)
        self.popupMenu.addAction(self.actionClear)
        self.actionClear.triggered.connect(self.__onActionClearTriggered)

    def mousePressEvent(self, event):
        super().mousePressEvent(event)
        if event.button() == Qt.RightButton:
            # 显示右键菜单
            self.popupMenu.exec_(event.globalPos())

    def __onActionClearTriggered(self):
        # 菜单选项action1的动作
        self.clear()

    def appendLog(self, msg: str, color: QColor):
        if self.count() == self.__maxLogLineNum:
            self.takeItem(0)
        item = QListWidgetItem(msg)
        item.setForeground(color)
        self.addItem(item)
        self.scrollToBottom()

    def SetMaxLineNum(self, maxLineNum: int):
        self.__maxLogLineNum = maxLineNum



